import 'package:flutter/material.dart';
import 'package:play_android/com/zhihaoliang/const/AppStr.dart';
import 'package:play_android/com/zhihaoliang/http/HttpException.dart';
import 'package:play_android/com/zhihaoliang/play/login/view/LoginPage.dart';
import 'package:play_android/com/zhihaoliang/util/ToastUtil.dart';
import 'package:play_android/com/zhihaoliang/widget/LoadingDialog.dart';

class BaseState {
  LoadingDialog _loadingDialog;

  showLoadingDialog(BuildContext context, {String msg = AppStr.LOADING}) {
    showDialog<Null>(
        context: context, //BuildContext对象
        barrierDismissible: true,
        builder: (BuildContext context) {
          _loadingDialog = new LoadingDialog(
            //调用对话框
            text: msg,
          );
          return _loadingDialog;
        });
  }

  dismissLoadingDialog(BuildContext context) {
    if (_loadingDialog != null) {
      _loadingDialog.dismiss(context);
    }
  }

  @override
  void onTipError(
      HttpException httpException, BuildContext context, String tip) {
    dismissLoadingDialog(context);
    if (httpException.errorCode == -1001) {
      tip = AppStr.LOGIN_TIMEOUT;
    }
    String msg = httpException.message;
    msg = msg == null ? tip : msg;
    ToastUtil.showError(msg);

    if (httpException.errorCode == -1001) {
      Navigator.of(context).push(MaterialPageRoute(builder: (context) {
        return LoginPage();
      }));
    }
  }
}
